<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<h1>JavaScript Example 11</h1>
<br>
Mini Calculator - Shows how to call functions from events
<br>
<hr>
<br>

<!-- FORM -->
<form name="calculator" action="">
<input name="input" type="text" size="8" onchange="calculate()"/>
<select name="operation" onchange="calculate()">
	<option>square</option>
	<option>double</option>
	<option>factorial</option>
</select>
=
<input name="output" type="text" size="15"/>
</form>


<!-- SCRIPT STARTS HERE -->

<script type="text/javascript">
	// define factorial function
	function factorial(x) {
		return x==1?1:factorial(x-1)*x;
	}
	// main calculate function
	function calculate() {
		// read form values !
		var value = document.calculator.input.value;
		var opindex = document.calculator.operation.selectedIndex;
		var result;
		if (isNaN(value)) result = "not defined";
		else {
			if (opindex==0) result = value*value;
			if (opindex==1) result = value*2;
			if (opindex==2) result = factorial(value);
		}
		// write form values
		document.calculator.output.value = result;
	}
</script>

<!-- SCRIPT ENDS HERE -->

</body>
</html>
